//
// Created by Semir on 2020/5/11.
//

#include <iostream>
#include <vector>

using namespace std;

int dirX[4] = {0, -1, 0, 1};
int dirY[4] = {1, 0, -1, 0};

bool check(vector<vector<int>>& mp, int threshold, int x, int y) {
    if (mp[x][y] >= 0) {
        return mp[x][y] > 0;
    }
    int x0 = x, y0 = y;
    int sum = 0;
    while (x) {
        sum += x % 10;
        x /= 10;
    }
    while (y) {
        sum += y % 10;
        y /= 10;
    }
    bool res = threshold >= sum;
    mp[x0][y0] = res ? 1 : 0;
    return res;
}

void dfs(vector<vector<bool>>& vis, vector<vector<int>>& mp, int threshold, int rows, int cols, int x, int y, int& cnt) {
    if (!check(mp, threshold, x, y)) {
        return;
    }
    cnt++;
    for (int i = 0; i < 4; i++) {
        int dx = x + dirX[i];
        int dy = y + dirY[i];
        if (dx >= 0 && dx < rows && dy >= 0 && dy < cols && !vis[dx][dy]) {
            vis[dx][dy] = true;
            dfs(vis, mp, threshold, rows, cols, dx, dy, cnt);
        }
    }
}

int movingCount(int threshold, int rows, int cols) {
    vector<vector<bool>> vis(rows, vector<bool>(cols));
    vector<vector<int>> mp(rows, vector<int>(cols, -1));
    int x = 0, y = 0, cnt = 0;
    vis[x][y] = true;
    dfs(vis, mp, threshold, rows, cols, x, y, cnt);
    return cnt;
}

int main() {

    return 0;
}